package com.test.learnkotlin.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.test.learnkotlin.bean.BaseInfoType

class BaseInfoLayout (context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
//    constructor(context: Context?) : super(context)
//
//    constructor(context: Context?, attributeSet: AttributeSet) : super(context, attributeSet)
//
//    constructor(context: Context?, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)
//
//    constructor(context: Context?, attributeSet: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attributeSet, defStyleAttr, defStyleRes)

    private var itemData: MutableList<PeopleDataInterface>
    private var baseInfoTypes: MutableList<BaseInfoType>

    init {
        orientation = VERTICAL
        itemData = ArrayList()
        val capaccity = 7
        baseInfoTypes = ArrayList(capaccity)


        for (i in 0..capaccity) {

            var type: Int = WEIGHT_TYPE_SIGNLE
            var name = "$i 请选择 "
            var value: String? = null
            var position = i

            if (i == 0 || i == 3 || i == 4 || i == 6) {
                type = WEIGHT_TYPE_EDIT
                name = "$i 请输入 "
            }
            baseInfoTypes.add(BaseInfoType(type, name, value, position))

        }
    }

    fun initData() {
        removeAllViews()
        itemData.clear()
        val responseSize = baseInfoTypes.size
        var i = 0
        while (i < responseSize) {
            var baseInfoType = baseInfoTypes.get(i)

            if (baseInfoType.type == WEIGHT_TYPE_EDIT) {
                val editItem = EditItem(context)
                editItem.setItemData(baseInfoType)
//                editItem.et_item_value.setOnKeyListener { v, keyCode, event ->
//                    LogUitls.i("editItem.et_item_value 键盘事件回调")
//                    if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
//                        LogUitls.i("editItem.et_item_value  回车键")
//                    }
//
//                    false
//                }
                addView(editItem)

//                val view = inflate(context, R.layout.item_edit, null)
//               val  et_item_value: EditText = view.findViewById(R.id.et_item_value)
//                et_item_value.isSingleLine = true
//                et_item_value.setOnKeyListener { v, keyCode, event ->
//                    LogUitls.i("et_item_value 键盘事件回调")
//                    if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
//                        LogUitls.i("et_item_value 回车键")
//                    }
//
//                    false
//                }
//
//
//                addView(view)

            } else if (baseInfoType.type == WEIGHT_TYPE_SIGNLE) {
                val singleItem = SingleItem(context)
                singleItem.setItemData(baseInfoType)
                addView(singleItem)
            }

            i++

        }

    }

    companion object {
        const val WEIGHT_TYPE_EDIT = 1
        const val WEIGHT_TYPE_SIGNLE = 2
    }
}